<template>
  <div class="padding-left-right-20 padding-top-20">
    <div>
      <el-form ref="searchForm" size="mini" label-position="left" :model="formInline" class="hidden-box" label-width="100px" @submit.native.prevent>
        <el-row :gutter="10">
          <el-col :span="7">
            <el-form-item label="流程标题">
              <el-input size="small" class="input-box" v-model="formInline.processName" placeholder="请搜索流程名称" @keyup.enter.native="getInfo(1)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="业务类型">
              <el-select size="small" class="full-width" v-model="formInline.processType" placeholder="请选择" clearable @change="getInfo(1)">
                <el-option v-for="(item, index) in typeList" :key="index" :label="item.value" :value="item.key"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="关联企业">
              <el-select size="small" class="full-width" v-model="formInline.enterpriseCode" placeholder="请选择" clearable @change="getInfo(1)">
                <el-option v-for="(item, index) in firmList" :key="index" :label="item.enterpriseName" :value="item.enterpriseCode"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <div class="flex f-right">
                <el-button type="primary" size="mini" @click="getInfo(1)" class="margin-left-10">搜索</el-button>
                <el-button type="primary" size="mini" @click="createFlow">新增</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <el-table :data="list" size="medium" @row-click="management" :border="false" :show-header="false">
      <el-table-column label="流程名称" align="left" prop="processName" />
      <!-- <el-table-column label="关联表单" align='right'>
    <template slot-scope="scope">
    <span class="common-table-item">关联表单：{{scope.row.formName ? scope.row.formName : '暂无'}}</span>
    </template>
    </el-table-column> -->
      <el-table-column label="类型" width="100" align="right">
        <template slot-scope="scope">
          <span class="common-table-item">类型：{{ process[scope.row.processType - 1] | formduty }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更改时间" width="180" align="right">
        <template slot-scope="scope">
          <span class="common-table-item">更改时间：{{ scope.row.updateTime | formatDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="right">
        <template slot-scope="scope">
          <el-button type="text" @click.stop="deleteItem(scope.row.primaryKey)">删除</el-button>
          <el-button type="text" @click.stop="coptyItem(scope.row.primaryKey)">复制</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-if="queryParams.total > queryParams.pageSize" class="t-right margin-top-10" prev-text="上一页" next-text="下一页" layout="prev, pager, next" :total="queryParams.total" :current-page.sync="queryParams.currentPage" :page-size="queryParams.pageSize" @current-change="getInfo" />

    <el-dialog title="复制流程" :visible.sync="dialog.visible" width="50%" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false">
      <el-form ref="dialogForm" :model="dialog.model" :rules="dialog.rules" label-width="100px" size="small" label-position="right">
        <el-row>
          <el-col>
            <el-form-item label="流程名称" prop="processName">
              <el-input maxlength="50" autocomplete="off" clearable v-model.trim="dialog.model.processName" placeholder="请输入流程名称" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="addBtn">取 消</el-button>
        <el-button size="small" type="primary" @click="handleSave"> 确 定 </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { procConfigsList, procConfigsDelete, apiGetFirmList, procConfigsClone } from '../../api/process';

  export default {
    data() {
      return {
        queryParams: {
          currentPage: 1,
          pageSize: 20,
          total: 0,
        },
        firmList: [],
        formInline: {
          processName: '',
          processType: '',
          enterpriseCode: '',
        },
        list: [],
        process: ['人事', '财务', '业务', '营销', '行政', '成本', '员工注册（公司唯一）', '员工出差（公司唯一）'],
        typeList: [
          { key: 1, value: '人事' },
          { key: 2, value: '财务' },
          { key: 3, value: '业务' },
          { key: 4, value: '营销' },
          { key: 5, value: '行政' },
          { key: 6, value: '成本' },
          { key: 7, value: '员工注册（公司唯一）' },
          { key: 7, value: '员工出差（公司唯一）' },
        ],
        dialog: {
          visible: false,
          model: {
            processName: '',
            processCode: '',
          },
          rules: {
            processName: [{ required: true, message: '请输入流程名称', trigger: 'blur' }],
          },
        },
      };
    },
    mounted() {
      this.getFirmList();
      this.getInfo();
    },
    filters: {
      formduty(val) {
        switch (val) {
          case '1':
            return '人事';
          case '2':
            return '财务';
          case '3':
            return '业务';
          case '4':
            return '营销';
          case '5':
            return '行政';
          case '6':
            return '成本';
          case '7':
            return '员工注册（公司唯一）';
          case '8':
            return '员工出差（公司唯一）';
          default:
            return val;
        }
      },
    },
    methods: {
      //企业
      async getFirmList() {
        const res = await apiGetFirmList();
        this.firmList = res.enterprise || [];
      },
      getRandom(num) {
        var random = Math.floor((Math.random() + Math.floor(Math.random() * 9 + 1)) * Math.pow(10, num - 1));
      },
      createFlow() {
        let pathUrl = {
          name: 'flow-info',
        };
        this.$router.push(pathUrl);
      },
      management(val) {
        let pathUrl = {
          name: 'flow-info',
          query: {
            key: val.primaryKey,
            primaryKey: val.formKey,
            strucCode: val.bizStructure,
            processName: val.processName,
          },
        };
        let url = this.$router.resolve(pathUrl);
        window.open(url.href, '_blank');
      },
      //列表
      async getInfo(currentPage = 1) {
        // this.queryParams.currentPage = currentPage || this.queryParams.currentPage
        try {
          let param = {
            processType: this.formInline.processType || null,
            processName: this.formInline.processName || null,
            enterpriseCode: this.formInline.enterpriseCode || null,
            pageSize: this.queryParams.pageSize,
            currentPage: this.queryParams.currentPage,
          };
          const res = await procConfigsList(param);
          this.list = res.rows || [];
          this.queryParams.total = res.total;
        } catch (err) {
          console.log(err);
        }
      },
      //删除
      deleteItem(key) {
        this.$confirm('确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            let res = await procConfigsDelete({ primaryKey: key });
            if (res.success) {
              this.getInfo();
              this.$message.success('删除成功!');
            } else {
              this.$message.error(res.message);
            }
          })
          .catch(() => {});
      },
      coptyItem(item) {
        this.addBtn(item);
      },
      addBtn(primaryKey) {
        if (this.dialog.visible) {
          this.$refs.dialogForm.clearValidate();
        }
        this.dialog.visible = !this.dialog.visible;
        if (this.dialog.visible) {
          this.dialog.model.processCode = primaryKey;
          this.dialog.model.processName = '';
        }
      },
      async handleSave() {
        await this.$refs.dialogForm.validate();
        if (this.ISPOST) {
          this.$message({
            message: '操作太频繁！',
            type: 'warning',
          });
          return;
        }

        this.ISPOST = true;
        setTimeout(() => {
          this.ISPOST = false;
        }, 3000);
        let res = await procConfigsClone(this.dialog.model);
        if (res.success) {
          this.$message.success('保存成功！');
          this.dialog.visible = false;
          this.getInfo(1);
        } else {
          this.$message.error(res.message);
        }
      },
    },
  };
</script>
<style lang="scss" scoped>
  .demo-form-inline {
    margin-bottom: 50px;
  }
</style>
